﻿package mortal.game.view.warehouse
{
    import Message.Game.*;
    import Message.Public.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.pack.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class WarehouseController extends Controller
    {
        private var _warehouseModule:WarehouseModule;
        private var _warehouseCache:WarehouseCache;

        public function WarehouseController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._warehouseCache = cache.pack.warehouseCache;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._warehouseModule == null)
            {
                this._warehouseModule = new WarehouseModule();
                this._warehouseModule.addEventListener(WindowEvent.SHOW, this.onWinShow);
                this._warehouseModule.addEventListener(WindowEvent.CLOSE, this.onWinClose);
            }
            return this._warehouseModule;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.WarehouseOpen, this.openWin);
            Dispatcher.addEventListener(EventName.Warehouse_DragInItem, this.dragInItemHandler);
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            this.updateCapacity();
            this.updateWarehouse();
            this._warehouseModule.addEventListener(MouseEvent.MOUSE_OVER, this.mouse_over);
            this._warehouseModule.addEventListener(MouseEvent.ROLL_OUT, this.mouse_out);
            NetDispatcher.addCmdListener(ServerCommand.UpdateWarehouse, this.updateWarehouse);
            NetDispatcher.addCmdListener(ServerCommand.WarehouseGrid, this.updateCapacity);
            NetDispatcher.addCmdListener(ServerCommand.WarehouseUpdateByVip, this.updateCapacity);
            Dispatcher.addEventListener(EventName.WarehouseClose, this.winClosed);
            Dispatcher.addEventListener(EventName.WarehouseShowUnLock, this.showUnLock);
            Dispatcher.addEventListener(EventName.WarehouseHideUnLock, this.hideUnLock);
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            this._warehouseModule.removeEventListener(MouseEvent.MOUSE_OVER, this.mouse_over);
            this._warehouseModule.removeEventListener(MouseEvent.MOUSE_OUT, this.mouse_out);
            CursorManager.currentCurSorType = CursorManager.Arrow;
            NetDispatcher.removeCmdListener(ServerCommand.UpdateWarehouse, this.updateWarehouse);
            NetDispatcher.removeCmdListener(ServerCommand.WarehouseGrid, this.updateCapacity);
            NetDispatcher.removeCmdListener(ServerCommand.WarehouseUpdateByVip, this.updateCapacity);
            Dispatcher.removeEventListener(EventName.WarehouseClose, this.winClosed);
            Dispatcher.removeEventListener(EventName.WarehouseShowUnLock, this.showUnLock);
            Dispatcher.removeEventListener(EventName.WarehouseHideUnLock, this.hideUnLock);
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            if (!GameController.warehouse.isViewShow)
            {
                LayerManager.windowLayer.tweenCenterWindow(this.view, GameController.pack.view);
            }
            else
            {
                this._warehouseModule.hide();
            }
            return;
        }// end function

        private function updateWarehouse(param1:Object = null) : void
        {
            this._warehouseModule.updateAllItems();
            return;
        }// end function

        private function updateCapacity(param1:Object = null) : void
        {
            this._warehouseModule.updateCapacity();
            this._warehouseModule.updateAllItems();
            return;
        }// end function

        private function dragInItemHandler(event:DataEvent) : void
        {
            var _loc_2:DragDropData = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:String = null;
            var _loc_7:ItemData = null;
            var _loc_8:ItemData = null;
            var _loc_9:int = 0;
            var _loc_10:Boolean = false;
            var _loc_11:Object = null;
            var _loc_12:PackPosTypeCache = null;
            _loc_2 = event.data as DragDropData;
            var _loc_6:* = _loc_2.fromItemData;
            switch(_loc_2.fromPosType)
            {
                case EPlayerItemPosType._EPlayerItemPosTypeBag:
                {
                    _loc_11 = new Object();
                    _loc_11.pos = EMoveType._EMoveTypeToWarehouse;
                    _loc_11.formPos = _loc_2.fromPosType;
                    _loc_11.item = _loc_6;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.WarehouseToPack, _loc_11));
                    break;
                }
                case EPlayerItemPosType._EPlayerItemPosTypeWarehouse:
                {
                    _loc_12 = Cache.instance.pack.getPackChacheByPosType(_loc_2.fromPosType);
                    _loc_3 = _loc_12.getIndexByUid(_loc_2.uid);
                    _loc_4 = _loc_2.toIndex;
                    _loc_2.fromIndex = _loc_3;
                    _loc_2.toIndex = _loc_4;
                    if (_loc_4 <= _loc_12.sbag.capacity)
                    {
                        if (_loc_12.changeItemPos(_loc_2))
                        {
                            this.updatePackHandler();
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function updatePackHandler(param1:Object = null) : void
        {
            this._warehouseModule.updateAllItems();
            return;
        }// end function

        private function winClosed(event:DataEvent) : void
        {
            this._warehouseModule.hide();
            return;
        }// end function

        private function showUnLock(event:DataEvent) : void
        {
            this._warehouseModule.showUnLockItem(event.data as Object);
            return;
        }// end function

        private function hideUnLock(event:DataEvent = null) : void
        {
            if (this._warehouseModule)
            {
                this._warehouseModule.hideUnLockItem();
            }
            return;
        }// end function

        private function setSelectIndex(event:DataEvent) : void
        {
            this._warehouseCache.maxSelsecIndex = event.data as int;
            return;
        }// end function

        private function mouse_over(event:MouseEvent) : void
        {
            CursorManager.currentCurSorType = CursorManager.Arrow;
            CursorManager.currentCurSorType = CursorManager.GET;
            CursorManager.showCursor(CursorManager.GET);
            return;
        }// end function

        private function mouse_out(event:MouseEvent) : void
        {
            CursorManager.showCursor(CursorManager.Arrow);
            return;
        }// end function

    }
}
